package com.sikadwafastdaisfondturbo.pretloan.pro

import android.view.View
import com.sikadwafastdaisfondturbo.pretloan.base.BaseActivity
import com.sikadwafastdaisfondturbo.pretloan.utils.config.Configs
import com.sikadwafastdaisfondturbo.pretloan.utils.dialog.KefuDialog
import com.sikadwafastdaisfondturbo.pretloan.utils.dialog.PersonalDialog
import com.sikadwafastdaisfondturbo.pretloan.utils.helper.StarDialogHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.http.AppUrls
import com.sikadwafastdaisfondturbo.pretloan.utils.http.AppUrls.addCommonParams
import com.sikadwafastdaisfondturbo.pretloan.utils.http.HttpHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.http.IHttpCallback
import com.sikadwafastdaisfondturbo.pretloan.utils.log.HHLog
import org.json.JSONObject
import java.io.File

/**
 * Sikadwa
 * @Author wwdeng
 * @CreateTime: 2024/5/20 16:10
 * @Annotation：
 */
abstract class ProActivity: BaseActivity() {

    //是否需要隐藏加载框
    var isNeedHideLoading = true
    var mKefuDialog: KefuDialog? = null

    /**
     * 获取APP配置信息
     */
    fun getCommonObject(): JSONObject{
        return JSONObject(Configs.getSPString(Configs.SPKEY.SIKADWA_COMMON))
    }

    /**
     * 显示客服dialog
     */
    fun showKefuDialog(eventRemark: String){
        if (mKefuDialog != null && mKefuDialog!!.isShowing){
            return
        }
        if (mKefuDialog == null){
            mKefuDialog = KefuDialog(this,eventRemark,object : KefuDialog.IDialogCallback{
                override fun onClick(view: View?) {
                }
            })
            mKefuDialog!!.setCancelable(true)
            mKefuDialog!!.setCanceledOnTouchOutside(false)
            mKefuDialog!!.show()
            mKefuDialog!!.loadKefuContactAction()
            return
        }
        mKefuDialog!!.show()
    }

    /**
     * 显示个人中心dialiog
     */
    fun showPersonalDialog(eventRemark: String){
        val dialog = PersonalDialog(this,eventRemark,object : PersonalDialog.IDialogCallback{
            /**
             * 打分
             */
            override fun onRateUsClick() {
                val httpUrl = AppUrls.getHttpUrl(AppUrls.URL.COMMON)
                val params = JSONObject().addCommonParams().apply {
                    put("path", AppUrls.PATH.STAR_COMMENT)
                }
                postAction(httpUrl,params,object : IHttpCallback {
                    override fun onRequestSuccess(data: JSONObject) {
                        val obj = data.optJSONObject("obj")
                        StarDialogHelper.showStarDialog(this@ProActivity,obj,false)
                    }
                    override fun onRequestError(error: JSONObject) {
                    }
                    override fun onRequestFinish() {
                    }
                })
            }
        })
        dialog.show()
    }

    /**
     * 开始网络请求 Post
     * @param url       : 接口地址
     * @param params    : 参数
     * @param callback  :  回调
     */
    open fun postAction(url: String, params: JSONObject, callback: IHttpCallback) {
        HttpHelper.post(url, params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                val code = data.optInt("code")
                when(code){
                    0 -> {//成功
                        callback.onRequestSuccess(data)
                    }
                    else -> {//失败
                        val msg = data.optString("msg")
                        showToast(msg)

                        callback.onRequestError(data)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                callback.onRequestError(error)
                HHLog.e("error：$error")
                showToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
                if (isNeedHideLoading){
                    hideLoading()
                }
            }
        })
    }

    /**
     * POST方式请求 (文件上传)
     *
     * @param url
     * @param file
     * @param callback
     */
    open fun uploadFileAction(url: String, file: File, callback: IHttpCallback){
        HttpHelper.uploadFile(url,file,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                val code = data.optInt("code")
                when(code){
                    0 -> {//成功
                        callback.onRequestSuccess(data)
                    }
                    else -> {//失败
                        val msg = data.optString("msg")
                        showToast(msg)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                HHLog.e("error：$error")
                showToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                callback.onRequestFinish()
                if (isNeedHideLoading){
                    hideLoading()
                }
            }
        })
    }

    /**
     * 添加埋点
     */
    open fun addEvent(eventName: String,remark: String){
        HttpHelper.addEvent(eventName,remark)
    }
}